<?php
namespace app\admin\controller;

use app\common\model\Spec as SpecModel;
use app\common\controller\AdminBase;
use think\Db;

/**
 * 规格管理
 * Class Spec
 * @package app\admin\controller
 * author  lixy
 */
class Spec extends AdminBase
{

    protected $spec_model;

    protected function _initialize()
    {
        parent::_initialize();

        $this->spec_model  = new SpecModel();

    }

    /**
     * 规格管理
     * @return mixed
     */
    public function index()
    {
        $cate2_id  = input('cate2_id');
        $res = Db::name('spec')->where(['cate2_id'=>$cate2_id])->order('id', 'desc')->select();
        $this->assign('list',$res);
        $this->assign('cate2_id',$cate2_id);
        return $this->fetch();
    }

    /**
     * 添加规格
     */
    public function addSpec(){

        $data  = input();
        $data['ctime'] = time();
        $res = Db::name('spec')->insert($data);
        if($res){
            $this->success('添加成功！');
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 编辑规格
     */
    public function editSpec(){
        $id = input('id');
        if($id){
            if($this->request->isPost()){

                $data['name'] = input('name');
                $res = Db::name('spec')->where(['id'=>$id])->setField('name',input('name'));
                if($res){
                    $this->success('编辑成功');
                }else{
                    $this->error('编辑错误');
                }

            }else{
                $res = Db::name('spec')->where(['id'=>$id])->find();
                $this->assign('find',$res);
                return $this->fetch();
            }
        }else{
            $this->error('参数错误');
        }

    }


    /**
     * 删除规格
     */
    public function delSpec(){
        $id = input('id');
        if($id){
            $res = Db::name('spec')->delete($id);
            if($res){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }

        }else{
            $this->error('参数错误');
        }
    }


}